<?php
/**
 * Created by PhpStorm.
 * User: hhcycj
 * Date: 2018/4/4
 * Time: 11:32
 */

// http://local.hhcycj.com:8811/index.html?username=hxslcc&age=29

/**
 * 正常运行php流程：客户端发起请求 -> nginx -> fpm(调度fastcgi)
 * 如果其用swoole的httpServer：客户端发起请求 -> nginx -> httpsServer
 *
 * Swoole\Http\Server 继承 Swoole\Server
 */

$http = new swoole_http_server("0.0.0.0", 8811);

//设置document_root并设置enable_static_handler为true后，
//底层收到Http请求会先判断document_root路径下是否存在此文件，
//如果存在会直接发送文件内容给客户端，不再触发onRequest回调。
$http->set([
    'enable_static_handler' => true,  // 允许静态文件
    'document_root'         => '/var/www/swoole/data', // 静态资源路径
    'worker_num'            => 8,
]);

$http->on('request', function ($request, $response) {
    /*if ($request->server['request_uri'] =='/favicon.ico'){
         $response->status(404);
         $response->end(Util::show(-1, 'unknow error.'));
         return;
    }*/


    //var_dump($request->server, $request->get, $request->post);
    $response->header("Content-Type", "text/html; charset=utf-8");
    $response->cookie("name", "hxslcc", time() + 18000);
    $response->cookie("age", "29", time() + 18000);

    //发送Http响应体，并结束请求处理。
    $response->end("<h1>Hello Swoole. #" . rand(1000, 9999) . "</h1>\n" . "GET DATA: " . json_encode($request->get));
});

$http->start();